﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace FTchina.PureHtml
{
    /// <summary>
    ///CreatHtml 的摘要说明
    /// </summary>
    public class CreatHtml
    {
        public string id = string.Empty;
        public int cid = 0;
        public int pageindex = 0;
        NewsBody NB = new NewsBody();
        public CreatHtml()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

      
        public void CreatOneHtml(string newsid, int newsidcid, int newsidpageindex)
        {
             id = newsid;
            cid =newsidcid;
            pageindex = newsidpageindex;


            
            VelocityHelper vh = new VelocityHelper();
            vh.Init("site/templates/");
            FTchina.PureHtml.BasicInfo bi = new FTchina.PureHtml.BasicInfo();
 
            bi.PutAll(vh);
 

            
            vh.Put("ClassId", cid.ToString());
            vh.Put("NewsId", id.ToString());
             ShowNews();
            vh.Put("NewsBody", NB);
            vh.CreatHtml("show.html", basic.GetMapPath()+(FTchina.Api.htmlpath.GetHtmlPath(id, pageindex.ToString(), NB.MakeHtmlDate)));

            LogNet.LogBLL.debug("Done"+newsid);

        }

        protected void ShowNews()
        {
 
             string sql = "select * from content where id=" + id;
            if (shujuku.testread("select id from content where id=" + id))
            {
                DataRow dr = shujuku.GetDataTable(sql).Rows[0];
                //如果有外部跳转链接则直接跳转
                if (dr["url"].ToString().Trim() != "")
                {
                    return;
                }
                NB.Title = dr["title"].ToString();
                NB.Date = FTchina.DataTimeFormat.FormatTime(dr["data"].ToString(), "1");
                NB.Author = dr["author"].ToString();
                NB.MakeHtmlDate = dr["data"].ToString();

                if (pageindex == 1)
                {
                    NB.Content = dr["content"].ToString();
                    NB.PageTitle = NB.Title;
                    NB.PageFenye = string.Empty;
                }
                else
                {
                    DataTable dtpages = shujuku.GetDataTable("select * from contentpage where artid='" + id + "' and pageindex='" + pageindex + "'");
                    NB.Content = dtpages.Rows[0]["pagecontent"].ToString();
                    NB.PageTitle = dtpages.Rows[0]["pagetitle"].ToString();

                }
                NB.PageFenye = showPages(NB.PageTitle);


                NB.ClassName = dr["lanmuname"].ToString();
                NB.JianJie = dr["jianjie"].ToString();

                NB.PreviousLink = FTchina.Api.NewsList.Getlink(id, "up");
                NB.NextLink = FTchina.Api.NewsList.Getlink(id, "");

            }
            else
            {
                return;
            }

        }

        protected DataSet ShowFenyeNewsContet(string pagetitle1)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            DataColumnCollection columns = dt.Columns;
            columns.Add("pagetitle", typeof(System.String));
            columns.Add("pageindex", typeof(System.String));


            DataRow dr1 = dt.NewRow();
            dr1["pagetitle"] = pagetitle1;
            dr1["pageindex"] = "1";
            dt.Rows.Add(dr1);


            DataTable page = shujuku.GetDataTable("select * from contentpage where artid=" + id);
            for (int i = 0; i < page.Rows.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["pagetitle"] = page.Rows[i]["pagetitle"];
                dr["pageindex"] = page.Rows[i]["pageindex"];
                dt.Rows.Add(dr);
            }
            ds.Tables.Add(dt);
            return ds;
        }

        private string showPages(string pagetitle1)
        {
            string tempwords = string.Empty;
            int CurrentPage = pageindex;
            DataSet ds = ShowFenyeNewsContet(pagetitle1);

            if (ds.Tables[0].Rows.Count > 1)
            {
                tempwords = ShwPagesLinks(ds, CurrentPage);
            }
            return tempwords;
        }
        private string ShwPagesLinks(DataSet ds, int CurrentPage)
        {

            int PageCount = ds.Tables[0].Rows.Count;

            //上一页
            string SyyFlag = (CurrentPage > 1) ? "<li><a href=" + FTchina.Api.htmlpath.GetFullPath(id, (CurrentPage - 1).ToString()) + ">上一页</a></li>" : "";
            //下一页
            string XyyFlag = (CurrentPage < PageCount) ? "<li><a href=" + FTchina.Api.htmlpath.GetFullPath(id, (CurrentPage + 1).ToString()) + " >下一页</a></li>" : "";

            string tempwords = "<div class=\"pager\"><ul><li class=\"p_total\">" + CurrentPage + "/" + PageCount + "</li>" + GetrelatedPage(CurrentPage, PageCount, 5, cid.ToString()) + SyyFlag + XyyFlag + "</ul>";
            tempwords += "<li><select class=\"p_total\" name=\"menu1\" onchange=\"javascript:window.location=(this.options[this.selectedIndex].value)\"> <option value=#>页内导航</option>";

            for (int i = 1; i < PageCount + 1; i++)
            {
                tempwords += "<option value=" + FTchina.Api.htmlpath.GetFullPath(id, i.ToString()) + ">" + ds.Tables[0].Rows[i - 1]["pagetitle"] + "</option>";
            }
            tempwords += "</select></li>";
            tempwords += "</div>";
            return tempwords;
        }

        private string GetrelatedPage(int CurrentPage, int PageCount, int number, string cid)
        {
            string tempwords = string.Empty;
            int CountSmall = (CurrentPage - number > 0) ? CurrentPage - number : 1;
            int CountBig = ((CurrentPage + number) > PageCount) ? PageCount + 1 : CurrentPage + number;
            for (int i = CountSmall; i < CountBig; i++)
            {
                if (i == CurrentPage)
                {
                    tempwords += "<li class=\"p_current\">" + i + "</li>";
                }
                else
                {
                    tempwords += "<li><a href=" + FTchina.Api.htmlpath.GetFullPath(id, i.ToString()) + " >" + i + "</a></li>";
                }
            }
            return tempwords;
        }


    }

    #region 新闻实体类
    public class NewsBody
    {
        private string _title = string.Empty;
        private string _date = string.Empty;
        private string _author = string.Empty;
        private string _content = string.Empty;
        private string _classname = string.Empty;
        private string _jianjie = string.Empty;
        private string _hit = string.Empty;
        private string _pageindex = string.Empty;
        private string _pagetitle = string.Empty;
        private string _previouslink = string.Empty;
        private string _nextlink = string.Empty;
        private string _pagefenye = string.Empty;
        private string _makehtmldate = string.Empty;
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
        public string Date
        {
            get { return _date; }
            set { _date = value; }
        }
        public string Author
        {
            get { return _author; }
            set { _author = value; }
        }
        public string Content
        {
            get { return _content; }
            set { _content = value; }
        }
        public string ClassName
        {
            get { return _classname; }
            set { _classname = value; }
        }
        public string JianJie
        {
            get { return _jianjie; }
            set { _jianjie = value; }
        }
        public string Hit
        {
            get { return _hit; }
            set { _hit = value; }
        }

        public string PageIndex
        {
            get { return _pageindex; }
            set { _pageindex = value; }
        }
        public string PageTitle
        {
            get { return _pagetitle; }
            set { _pagetitle = value; }
        }


        public string PreviousLink
        {
            get { return _previouslink; }
            set { _previouslink = value; }
        }

        public string NextLink
        {
            get { return _nextlink; }
            set { _nextlink = value; }
        }

        public string PageFenye
        {
            get { return _pagefenye; }
            set { _pagefenye = value; }
        }

        public string MakeHtmlDate
        {
            get { return _makehtmldate; }
            set { _makehtmldate = value; }
        }
    }
    #endregion
}


   